/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：FunNodeDaoImpl.java
 * History:
 *       2012-1-13: Initially created, tangkf.
 */
package com.xteam.purview.service.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;

import com.xteam.db.IBaseDao;
import com.xteam.purview.entities.PurviewFunNode;
import com.xteam.purview.service.AbsFace;
import com.xteam.purview.service.IPurviewFunNodeFace;
import com.xteam.utils.TreeNodeUtils;

/**
 * @author tangkf ---2012-1-13
 */
public class PurviewFunNodeFace extends AbsFace<PurviewFunNode> implements IPurviewFunNodeFace {
	
	private PurviewFunNodeFace() {
	};

	private static PurviewFunNodeFace	instance	= new PurviewFunNodeFace();

	public static PurviewFunNodeFace getInstance() {
		return instance;
	}

	/**
	 * 直接转为JSON数据
	 * @author tangkf ---2011-9-15
	 * @return
	 */
	public JSONArray getFunNodeJsonTreeNode() {
		JSONArray typeTreeList = null;
		try {
			List<PurviewFunNode> pfns = getAllFunNodes();
			typeTreeList = TreeNodeUtils.getJSONArrayTreeNode(pfns, "0");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return typeTreeList;
	}
	
	/**
	 * 返回父节点的所有孩子节点
	 * @author tangkf  ---2012-3-30
	 * @param pid
	 * @return
	 */
	public List<PurviewFunNode> getFunNodeListByPid(String pid){
		List<PurviewFunNode> list = null;
		String hql = IBaseDao.SQL_FROM + PurviewFunNode.REF + IBaseDao.SQL_WHERE + PurviewFunNode.PROP_PFUNID + "=?" + IBaseDao.SQL_ORDERBY
				+ PurviewFunNode.PROP_SORT;
		try {
			list = dao.queryAll(hql, new String[] { pid });
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 由节点ID列表查询出所有节点
	 * @author tangkf  ---2012-2-10
	 * @param funIds
	 * @return
	 */
	public List<PurviewFunNode> getFunNodeList(Collection<String> funIds) {
		List<PurviewFunNode> list = null;
		String hql = IBaseDao.SQL_FROM + PurviewFunNode.REF + IBaseDao.SQL_WHERE + PurviewFunNode.PROP_FUNID + IBaseDao.SQL_IN + "(:funIds)"
				+ IBaseDao.SQL_ORDERBY + PurviewFunNode.PROP_SORT;
		try {
			if (funIds != null && funIds.size() > 0) {
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("funIds", funIds);
				list = dao.queryAll(hql, param);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 按顺序返回所有功能节点
	 * @author tangkf ---2012-2-2
	 * @return
	 */
	public List<PurviewFunNode> getAllFunNodes() {
		List<PurviewFunNode> list = null;
		String hql = IBaseDao.SQL_FROM + PurviewFunNode.REF + IBaseDao.SQL_ORDERBY + PurviewFunNode.PROP_SORT;
		try {
			list = dao.queryAll(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 按顺序返回所有孩子功能节点
	 * @author tangkf ---2012-2-2
	 * @return
	 */
	public String getFunNodeJsonByPid(String pid) {
		List<PurviewFunNode> list = this.getFunNodeListByPid(pid);
		return JSONArray.fromObject(list).toString();
	}
	
	/**
	 * 返回指定用户有权限的功能节点表<br>
	 * 不包含用户所继承的角色所包含的功能节点
	 * @author tangkf ---2012-1-18
	 * @param userId
	 * @return
	 */
	public List<PurviewFunNode> getFunNodeListByUserId(String userId) {
		List<PurviewFunNode> list = null;
		String hql = IBaseDao.SQL_FROM + PurviewFunNode.REF + IBaseDao.SQL_WHERE + PurviewFunNode.PROP_PFUNID + "=?" + IBaseDao.SQL_ORDERBY
				+ PurviewFunNode.PROP_SORT;
		try {
			list = dao.queryAll(hql, new String[] { userId });
			System.err.println("userId=" + userId);
			System.err.println(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 批量递归删除
	 * @see com.xteam.purview.service.AbsFace#delBatch(java.lang.Class, java.lang.String, java.lang.String[])
	 */
	public int delBatch(Class<PurviewFunNode> clazz, String idName, String[] ids) {
		int r	= 0;
		if(ids!=null && ids.length>0){
			r	= super.delBatch(clazz, idName, ids);
			for(String id:ids){
				List<PurviewFunNode> clist	= this.getFunNodeListByPid(id);
				if(clist!=null && clist.size()>0){
					StringBuffer sids	= new StringBuffer("");
					for(PurviewFunNode fn:clist){
						sids.append(fn.getFunId()).append(",");
					}
					String[] cids	= sids.toString().split(",");
					this.delBatch(clazz, idName, cids);
				}
			}
		}
		return r;
	}

	/**
	 * 返回指定角色有权限的功能节点表
	 * @author tangkf ---2012-1-18
	 * @param userId
	 * @return
	 */
	public List<PurviewFunNode> getFunNodeListByRoleId(String roleId) {
		// TODO
		return null;
	}
}